<template>
  <view>
    <com-select
      v-model="selectArea"
      :dictList="areas"
      memo="请选择地址"
    ></com-select>
  </view>
</template>

<script>
import ComSelect from "../com-select/com-select.vue";

export default {
  components: { ComSelect },
  props: {
    modelValue: {
      type: [Array, String],
      default: "",
    },
  },
  data() {
    return {
      areas: [],
      selectArea: [],
    };
  },
  mounted() {
    this.selectArea = this.modelValue;
    this.$comStore.area.listArea().then((res) => {
      this.areas = transformTree(res[0].child);
    });
  },
  watch: {
    selectArea(val) {
      this.$emit("update:modelValue", val);
    },
  },
};

function transformTree(areas) {
  if (!areas) {
    return [];
  }
  return areas.map((item) => ({
    name: item.name,
    val: item.cityCode.substr(-6),
    children: transformTree(item.child),
  }));
}
</script>

<style></style>
